<?php

namespace app\controller\admin;

use app\Code;
use app\controller\BaseController;
use app\dao\TeacherDao;
use app\service\TeacherService;
use app\validate\ProjectValidate;
use app\validate\TeacherValidate;
use think\exception\ValidateException;
use think\facade\Log;
use think\facade\Request;

class TeacherController extends BaseController
{

    protected $teacherService;

    public function __construct()
    {
        $this->teacherService = new TeacherService();
    }

    //增加指导老师
    public function addTeacher()
    {

        $data = Request::post();
        validate(TeacherValidate::class)->check($data);
        try {

            $this->teacherService->addTeacher($data);

            return $this->returnJson();
        } catch (\Exception $e) {
            return $this->returnJson([], Code::NOT_FOUND, '增加指导老师失败');
        }

    }

    // 获取教师列表
    public function listTeacher()
    {
        try {

            $data = $this->teacherService->listTeachers();
            // 明确区分空数据和错误
            if ($data->isEmpty()) {
                return $this->returnJson([], Code::SUCCESS, '教师列表为空');
            }

            return $this->returnJson($data);
        }
        catch (\Exception $e) {

            return $this->returnJson([], Code::NOT_FOUND, '获取教师列表失败');
        }
    }

    // 获取单个教师详情
    public function getTeacher($id)
    {
        try {
            $this->teacherService->getTeacher($id);
            return $this->returnJson();
        } catch (\Exception $e) {

            return $this->returnJson([], Code::NOT_FOUND, '教师不存在或已被删除');
        }
    }

    //删除
    public function deleteTeacher($id)
    {


        try {

            $this->teacherService->deleteTeacher($id);

            return $this->returnJson();
        } catch (\Exception $e) {
            return $this->returnJson([], Code::NOT_FOUND, '删除指导老师失败');
        }

    }


    //修改
    public function updateTeacher($id)
    {
        $data = Request::put();


        try {

            $this->teacherService->updateTeacher($id, $data);

            return $this->returnJson();
        } catch (\Exception $e) {
            return $this->returnJson([], Code::NOT_FOUND, '修改指导老师失败');
        }
    }
}